/************************************************************************************/
/*  Program name CAPAMEIMP_CONDITIONS_USAGE.sql				                  	    */
/*  Creater:  CHAN Billy (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 10 FEB, 2006   CHAN Billy        First created                                   */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAMEIMP_CONDITIONS_USAGE';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_AME_CONDITIONS_USAGE;
		
	lv_COAID				NUMBER;
	lv_Conditions_Usage		NUMBER;
	
BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			
			INSERT INTO AME_CONDITION_USAGES
			VALUES
			(
			CAPBRIMP_MAPPING.GetAMERuleIDMapping(C1.RULE_ID),
			CAPBRIMP_MAPPING.GetAMERuleIDMapping(C1.CONDITION_ID),
			C1.CREATED_BY,
			C1.CREATION_DATE,
			C1.LAST_UPDATED_BY,
			C1.LAST_UPDATE_DATE,
			C1.LAST_UPDATE_LOGIN,
			C1.START_DATE,
			C1.END_DATE,
			C1.SECURITY_GROUP_ID
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;
	
	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/